module.exports = function ($scope, $resource, $state, $modal, toaster) {

	$scope.searchform = {};

    /* 分页
       * ========================================= */
    $scope.maxSize = 5;            //最多显示多少个按钮
    $scope.bigCurrentPage = 1;      //当前页码
    $scope.itemsPerPage = 10;         //每页显示几条

    $scope.load = function () {
        var para = {
            pageNo: $scope.bigCurrentPage,
            pageSize: $scope.itemsPerPage
        };

        angular.extend(para, $scope.searchform);

        $resource('/api/as/goc/infodiffusion/categorylist', {}, {}).save(para, function (res) {
            console.log(res);

            if (res.errcode !== 0) {
                toaster.error({title: '', body: res.errmsg});
                return;
            }

            $scope.objs = res.data.results;
            $scope.bigTotalItems = res.data.totalRecord;

        });
    };
    $scope.load();

    //创建
    $scope.create = function(obj){
        var para = $state.get('app.createinfotype');
        var resolve = {
            obj : function(){
                return obj;
            },
            what : function(){
                return 'create';
            },
        };
        angular.extend(para.resolve, resolve);

        var modalInstance = $modal.open(para);
        modalInstance.opened.then(function() {// 模态窗口打开之后执行的函数  
            console.log('modal is opened');  
        });  
        modalInstance.result.then(function(result) {  
            $scope.load();
        }, function(reason) {  
            console.log(reason);// 点击空白区域，总会输出backdrop  
            // click，点击取消，则会暑促cancel  
            $log.info('Modal dismissed at: ' + new Date());  
        }); 
    }

    //编辑
    $scope.edit = function(obj){
        var para = $state.get('app.createinfotype');
        var resolve = {
            obj : function(){
                return obj;
            },
            what : function(){
                return 'edit';
            },
        };
        angular.extend(para.resolve, resolve);

        var modalInstance = $modal.open(para);
        modalInstance.opened.then(function() {// 模态窗口打开之后执行的函数  
            console.log('modal is opened');  
        });  
        modalInstance.result.then(function(result) {  
            $scope.load();
        }, function(reason) {  
            console.log(reason);// 点击空白区域，总会输出backdrop  
            // click，点击取消，则会暑促cancel  
            $log.info('Modal dismissed at: ' + new Date());  
        }); 
    }

    //删除
    $scope.del = function(obj){
        if(confirm("确定要删除吗？")) {
            $resource('/api/as/goc/infodiffusion/createcategory', {}, {}).save({'id' : obj.id, 'del_flg' : '1'}, function(res){
                if (res.errcode === 0) {
                    toaster.success({title: '', body: '删除成功'});
                    $scope.load();
                } else {
                    toaster.error({title: '', body: res.errmsg});
                }
            })
        }
    }

};